/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 */

package controllers.search;


import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.lang.String.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//import javax.faces.bean.SessionScoped;
import java.util.StringTokenizer;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Course;
/**
 *
 * @author Christian
 */

@SessionScoped
@Named(value="courseSearch")
public class CourseSearch implements Serializable{
    //Oppretter lister av Courseobjekter
    private  List<List> metaCourses = new ArrayList<List>();
    private  List<Course> coursesUnsorted = new ArrayList<Course>(); // List of strings to be searched
    private  List<Course> coursesSorted = new ArrayList<Course>();
    private  List<Course> courseCodeSorted = new ArrayList<Course>();
    private  List<Course> courseNameSorted = new ArrayList<Course>();
    private  List<Course> courseLevelSorted = new ArrayList<Course>();
    private  List<Course> courseContentSorted = new ArrayList<Course>();
    private  List<Course> readingListSorted = new ArrayList<Course>();
    private  List<Course> replacesCourseSorted = new ArrayList<Course>();
    private  List<Course> additionalNotesSorted = new ArrayList<Course>();
    private  List<Course> lecturersSorted = new ArrayList<Course>();
    private  List<Course> cleanArray = new ArrayList<Course>();
    private final DatabaseHjelper dbh = DatabaseHjelper.hent();
    private List<String> searchList;
    private String searchString = "";

    public CourseSearch() {
        //updateCourses();
    }

     public String Search(){
        ClearArrays();
        updateCourses();
        //searchList();
        if(searchString.equalsIgnoreCase("")) return "searchResult";
        for(Course c: coursesUnsorted){
                if(c.getCourseCode().toLowerCase().contains(searchString)) courseCodeSorted.add(c);
                if(c.getCourseName().toLowerCase().contains(searchString)) courseNameSorted.add(c);
                if(c.getCourseLevel().toLowerCase().contains(searchString)) courseLevelSorted.add(c);
                if(c.getCourseContent().toLowerCase().contains(searchString)) courseContentSorted.add(c);
                if(c.getReadingList().toLowerCase().contains(searchString)) readingListSorted.add(c);
                if(c.getReplacesCourse().toLowerCase().contains(searchString)) replacesCourseSorted.add(c);
                if(c.getAdditionalNotes().toLowerCase().contains(searchString)) additionalNotesSorted.add(c);

         }
        return "searchResult";
    }


     //tømmer alle arrays for gammel data for å unngå duplikater
    public void ClearArrays(){
        metaCourses.clear();
        coursesUnsorted.clear();
        coursesSorted.clear();
        courseCodeSorted.clear();
        courseNameSorted.clear();
        courseLevelSorted.clear();
        courseContentSorted.clear();
        readingListSorted.clear();
        replacesCourseSorted.clear();
        additionalNotesSorted.clear();
        lecturersSorted.clear();
     }

    //lager en liste av søkeord basert på søkestrengen. Yay!
    public void searchList(){
       StringTokenizer st = new StringTokenizer(searchString.toLowerCase());
     while (st.hasMoreTokens()==true) {
         searchList.add(st.nextToken());
         //System.out.println("String tokenizer"+st.nextToken());
     }
    }

    private void updateCourses() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select * from course");
            res = stmt.executeQuery();
            coursesUnsorted.clear();
            while (res.next()){
                    Course c = new Course();
                    c.setCourseID(res.getInt("courseID"));
                    c.setCourseCode(res.getString("courseCode"));
                    c.setCourseName(res.getString("courseName"));
                    c.setCourseLevel(res.getString("courseLevel"));
                    c.setCourseCredit(res.getInt("courseCredits"));
                    c.setCredOverlapReduction(res.getInt("credOverlapReduction"));
                    c.setSemester(res.getString("semester"));
                    c.setTeachingLanguage(res.getString("teachingLanguage"));
                    c.setPrerequisites(res.getString("prerequisites"));
                    c.setCourseContent(res.getString("courseContent"));
                    c.setPedMethods(res.getString("pedMethods"));
                    c.setAssessment(res.getString("assessment"));
                    c.setGradingScale(res.getString("gradingScale"));
                    c.setUseOfExaminers(res.getString("useofexaminers"));
                    c.setReqResitExam(res.getString("reqResitExam"));
                    c.setExamAids(res.getString("examAids"));
                    c.setMandetoryWorkReq(res.getString("mandetoryWorkReq"));
                    c.setReadingList(res.getString("readingList"));
                    c.setReplacesCourse(res.getString("replacesCourse"));
                    c.setAdditionalNotes(res.getString("additionalNotes"));
                coursesUnsorted.add(c);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

// <editor-fold defaultstate="collapsed" desc="Getters and Setters. Expand to view code.">
    public void setSearchString(String searchString) {
        this.searchString = searchString.trim();
    }

    public String getSearchString() {
        return searchString;
    }

    public List<Course> getAdditionalNotesSorted() {
        return additionalNotesSorted;
    }

    public void setAdditionalNotesSorted(List<Course> additionalNotesSorted) {
        this.additionalNotesSorted = additionalNotesSorted;
    }

    public List<Course> getCourseCodeSorted() {
        return courseCodeSorted;
    }

    public void setCourseCodeSorted(List<Course> courseCodeSorted) {
        this.courseCodeSorted = courseCodeSorted;
    }

    public List<Course> getCourseContentSorted() {
        return courseContentSorted;
    }

    public void setCourseContentSorted(List<Course> courseContentSorted) {
        this.courseContentSorted = courseContentSorted;
    }

    public List<Course> getCourseLevelSorted() {
        return courseLevelSorted;
    }

    public void setCourseLevelSorted(List<Course> courseLevelSorted) {
        this.courseLevelSorted = courseLevelSorted;
    }

    public List<Course> getCourseNameSorted() {
        return courseNameSorted;
    }

    public void setCourseNameSorted(List<Course> courseNameSorted) {
        this.courseNameSorted = courseNameSorted;
    }

    public List<Course> getCoursesSorted() {
        return coursesSorted;
    }

    public void setCoursesSorted(List<Course> coursesSorted) {
        this.coursesSorted = coursesSorted;
    }

    public List<Course> getCoursesUnsorted() {
        return coursesUnsorted;
    }

    public void setCoursesUnsorted(List<Course> coursesUnsorted) {
        this.coursesUnsorted = coursesUnsorted;
    }

    public List<Course> getLecturersSorted() {
        return lecturersSorted;
    }

    public void setLecturersSorted(List<Course> lecturersSorted) {
        this.lecturersSorted = lecturersSorted;
    }

    public List<Course> getReadingListSorted() {
        return readingListSorted;
    }

    public void setReadingListSorted(List<Course> readingListSorted) {
        this.readingListSorted = readingListSorted;
    }

    public List<Course> getReplacesCourseSorted() {
        return replacesCourseSorted;
    }

    public void setReplacesCourseSorted(List<Course> replacesCourseSorted) {
        this.replacesCourseSorted = replacesCourseSorted;
    }

    public List<String> getSearchList() {
        return searchList;
    }

    public void setSearchList(List<String> searchList) {
        this.searchList = searchList;
    }
    
    public List<List> getMetaCourses() {
        return metaCourses;
    }
    // </editor-fold>
    
    //Boolean sjekkere som brukes til å avgjøre om en tabell skal rendres i view eller ikke
    // <editor-fold defaultstate="collapsed" desc="Booleans. Expand to view code.">
    public Boolean isCodeSortedEmpty(){
        if(courseCodeSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isCoursesSortedEmpty(){
        if(coursesSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isNameSortedEmpty(){
        if(courseNameSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isLevelSortedEmpty(){
       if(courseLevelSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isContentSortedEmpty(){
      if(courseContentSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isReadingListSortedEmpty(){
       if(readingListSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isRepCourseSortedEmpty(){
        if(replacesCourseSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isNotesSortedEmpty(){
        if(additionalNotesSorted.isEmpty())return true;
        else return false;
    }
    public Boolean isMetaCoursesEmpty(){
        if(searchString.equalsIgnoreCase("")) return false;
        if((courseCodeSorted.isEmpty()&&courseNameSorted.isEmpty()&&courseLevelSorted.isEmpty()&&courseContentSorted.isEmpty()&&readingListSorted.isEmpty()&&replacesCourseSorted.isEmpty()&&additionalNotesSorted.isEmpty()))return true;
        else return false;
    }
    public Boolean isSearchStringEmpty(){
        if(searchString.equalsIgnoreCase("")) return true;
        else return false;
    }
// </editor-fold>
}